﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Worktool.CODE_Chris.DataSourceHelpers;
using Worktool.CODE_Chris.SQL_DATA;

namespace Worktool
{
    public partial class ReportBedarfstermine : Form
    {
        private SQL sql;
        private ReportBedarfstermineHelper helper;
        private Boolean isInit = true;

        public ReportBedarfstermine()
        {
            InitializeComponent();
            this.Text = "Bedarfstermine: " + Login.currentDBName;
            init();
        }

        private void init()
        {
            sql = new SQL();
            helper = new ReportBedarfstermineHelper(reportViewer1, this);

            fillElementsInit();

            fillTablesInit();

            isInit = false;
        }

        #region Init
        private void fillTablesInit()
        {
            try
            {
                helper.getData1();
            }
            catch (Exception e)
            {

            }
        }

        private void fillElementsInit()
        {
            try
            {
                String[,] termine = sql.receiveSQL("SELECT COUNT(*) AS Amount FROM vw_rep_Bereitstellungsterminliste", "SELECT Bedarfstermin FROM vw_rep_Bereitstellungsterminliste ORDER BY Bedarfstermin");
                comboBox1.Items.Clear();
                comboBox1.DropDownHeight = 300;
                comboBox1.Items.Add("");
                for (int a = 0; a < termine.GetLength(1); a++)
                {
                    comboBox1.Items.Add(termine[0, a].Substring(0,10));
                }
                comboBox1.Refresh();
                comboBox1.Text = "";

                String[,] manufacturer = sql.receiveSQL("SELECT Manufacturer FROM vw_std_manufacturer");
                comboBox2.Items.Clear();
                comboBox2.DropDownHeight = 300;
                comboBox2.Items.Add("");
                for (int a = 0; a < manufacturer.GetLength(1); a++)
                {
                    comboBox2.Items.Add(manufacturer[0, a]);
                }
                comboBox2.Refresh();
                comboBox2.Text = "";
            }
            catch (Exception e)
            {

            }
        }
        #endregion

        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (comboBox2.Text == "")
                {
                    if (helper.getTable1() != null) helper.getTable1().DefaultView.RowFilter = string.Format("Bedarfstermin LIKE '%{0}%'", (comboBox1.Text));
                }
                else
                {
                    if (helper.getTable1() != null) helper.getTable1().DefaultView.RowFilter = string.Format("Bedarfstermin LIKE '%{0}%' AND Manufacturer LIKE '%{1}%'", comboBox1.Text, comboBox2.Text);
                }
                reportViewer1.RefreshReport();
            }
            catch (Exception ex)
            {
            }
        }

        private void comboBox2_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (comboBox1.Text == "")
                {
                    if (helper.getTable1() != null) helper.getTable1().DefaultView.RowFilter = string.Format("Manufacturer LIKE '%{0}%'", (comboBox2.Text + "*"));
                }
                else
                {
                    if (helper.getTable1() != null) helper.getTable1().DefaultView.RowFilter = string.Format("Bedarfstermin LIKE '%{0}%' AND Manufacturer LIKE '%{1}%'", comboBox1.Text, (comboBox2.Text + "*"));
                }
                reportViewer1.RefreshReport();
            }
            catch (Exception ex)
            {
            }
        }
    }
}
